# -*- coding: utf-8 -*-

# from urllib.request import urlopen
# from bs4 import BeautifulSoup
from selenium import webdriver
import csv
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
import datetime

csv_file = open("result.csv", "w", newline='')
writer = csv.writer(csv_file)
writer.writerow(['name', 'price', 'vip_price', 'count'])

url = 'https://coll.jd.com/list.html?sub=39866&delivery=0&page=0&JL=4_10_0'

# 浏览器参数，有无优化？！
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(executable_path=r"D:\Python\Python37\Scripts\chromedriver.exe", chrome_options=chrome_options)

while True:
    time_stamp = datetime.datetime.now()
    print("1 " + time_stamp.__str__())
    
    driver.get(url)
    
    time_stamp = datetime.datetime.now()
    print("2 " + time_stamp.__str__())
    
    data = driver.find_element_by_id("plist").find_elements_by_tag_name("li")
    
    time_stamp = datetime.datetime.now()
    print("3 " + time_stamp.__str__())
    
    for i in range(len(data)):
        divs = data[i].find_elements_by_tag_name("div")
        
        # 1
        name = divs[4].find_element_by_tag_name("em").text
#         print(name)
        
        # 2
        price = divs[3].find_element_by_tag_name("i").text
#         print(price)

        # 3
        try:
            vip_price = data[i].find_element_by_class_name("price-plus-1").find_element_by_tag_name("em").text
#         print(vip_price)
        except NoSuchElementException:
            vip_price = ""
        
        # 4
        count = divs[5].find_element_by_tag_name("a").text
#         print(count)
        
        # 写到excel
        writer.writerow([name, price, vip_price, count])
    
    time_stamp = datetime.datetime.now()
    print("4 " + time_stamp.__str__())
     
    try:
        btn_next = driver.find_element_by_class_name("pn-next")
        url = btn_next.get_attribute("href")
    except NoSuchElementException:
        break
    
    time_stamp = datetime.datetime.now()
    print("5 " + time_stamp.__str__())

driver.close()
csv_file.close()
exit()
